//
//  WKMnemonicsVeritfyVM.m
//  WuKongUsernameLogin
//
//  Created by tt on 2023/9/10.
//

#import "WKMnemonicsVeritfyVM.h"
#import "WKMnemonicsListCell.h"
#import "WKMnemonicsVerifyListCell.h"
#import <WuKongUsernameLogin/WuKongUsernameLogin-Swift.h>

@interface WKMnemonicsVeritfyVM ()

@end

@implementation WKMnemonicsVeritfyVM

- (NSArray<NSDictionary *> *)tableSectionMaps {
    if(!self.selectedMnemonics) {
        self.selectedMnemonics = [NSMutableArray arrayWithArray:@[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""]];
    }
    __weak typeof(self) weakSelf = self;
    return @[
        @{
            @"height":@(20.0f),
            @"items":@[
                    @{
                        @"class":WKLabelModel.class,
                        @"text":LLang(@"请根据您备份的助记词，按顺序选择填充"),
                        @"left": @(40),
                        @"width": @(WKScreenWidth - 80.0f),
                        @"center":@(true),
                        @"font":[WKApp.shared.config appFontOfSizeMedium:14.0f],
                        @"textColor": WKApp.shared.config.defaultTextColor,
                    },
            ],
        },
        @{
            @"height":@(20.0f),
            @"items":@[
                    @{
                        @"class":WKMnemonicsListModel.class,
                        @"items":self.selectedMnemonics,
                        @"editOn": @(true),
                        @"onItem": ^(NSInteger index) {
                            NSString *text = self.selectedMnemonics[index];
                            if(![text isEqualToString:@""]) {
                                self.selectedMnemonics[index] = @"";
                            }
                            [self reloadData];
                        }
                    },
            ],
        },
        @{
            @"height":@(20.0f),
            @"items":@[
                    @{
                        @"class":WKMnemonicsVerifyListModel.class,
                        @"mnemonics":self.mnemonics?:@[],
                        @"selectedMnemonics": self.selectedMnemonics,
                        @"onItem": ^(NSInteger index){
                            
                            NSString *clickText = self.mnemonics[index];
                            if(![clickText isEqualToString:@""] && [self.selectedMnemonics containsObject:clickText]) {
                                for (NSInteger i=0; i<self.selectedMnemonics.count; i++) {
                                    NSString *selectText = self.selectedMnemonics[i];
                                    if([selectText isEqualToString:clickText]) {
                                        self.selectedMnemonics[i] = @"";
                                        break;
                                    }
                                }
                            }else{
                                for (NSInteger i=0; i<self.selectedMnemonics.count; i++) {
                                    NSString *selectText = self.selectedMnemonics[i];
                                    if([selectText isEqualToString:@""]) {
                                        self.selectedMnemonics[i] = self.mnemonics[index];
                                        break;
                                    }
                                }
                            }
                            
                            
                            [self reloadData];
                        }
                    },
            ],
        },
        @{
            @"height":@(40.0f),
            @"items":@[
                    @{
                        @"class":WKButtonItemModel2.class,
                        @"title": LLang(@"完成"),
                        @"onPressed":^{
                            if(![self.selectedMnemonics isEqualToArray:self.orgMnemonics]) {
                                [WKNavigationManager.shared.topViewController.view showHUDWithHide:@"组记词顺序错误，请重新输入！"];
                                self.selectedMnemonics = [NSMutableArray arrayWithArray:@[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""]];
                                [self reloadData];
                                return;
                            }
                            NSString *loginUID = WKApp.shared.loginInfo.uid;
                            // 创建用户钱包
                            [WKWeb3Helper createUserWallet:[self.orgMnemonics componentsJoinedByString:@" "] uid:loginUID];
//
                            // 通过私钥获取用户公钥
                            NSString *privateKey = [WKWeb3Helper getUserPrivateKey:loginUID];
                            
                            NSString *publicKey = [WKWeb3Helper privateKeyToPublicKey:privateKey];
                            
                            // 上传公钥，并跳转到登录成功
                            [weakSelf uploadWeb3PublicKey:publicKey];
                            
//                            NSString *signStr = @"hello123";
//
//                            NSString *signContent =  [WKWeb3Helper signPersonalMessageWithPrivateKey:privateKey data:[signStr dataUsingEncoding:NSUTF8StringEncoding]];
//                            NSLog(@"signContent--->%@",signContent);
                        }
                    },
            ],
        },
    ];
}

-(void) uploadWeb3PublicKey:(NSString*)publicKey {
    UIView *topView = WKNavigationManager.shared.topViewController.view;
    [topView showHUD];
    [WKAPIClient.sharedClient POST:@"user/web3publickey" parameters:@{@"web3_public_key":publicKey}].then(^{
        [topView hideHud];
        WKApp.shared.loginInfo.extra[@"finishedWeb3Veritfy"] = @"1";
        [WKApp.shared.loginInfo save];
        [[WKApp shared] invoke:WKPOINT_LOGIN_SUCCESS param:nil];
    }).catch(^(NSError *error){
        [topView switchHUDError:error.domain];
    });
}


@end
